/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import BancoDados.Cliente;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Pablo
 */
@WebServlet(name = "ServletCliente", urlPatterns = {"/ServletCliente"})
public class ServletCliente extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletCliente</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletCliente at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        //processRequest(req, res);

        Cliente cliente = new Cliente();
        cliente = PreencherDados(req);

        String tipo = req.getParameter("tipo");

		StringBuffer output = new StringBuffer("");

        if (tipo.equals("inserir")) {

			boolean resultado;
			resultado = Cliente.Inserir(req);
			output.append(resultado);
        }
        if (tipo.equals("alterar")) {
            //Cliente.atualizar(cliente);

        } else if (tipo.startsWith("check")){
			String toCheckValue = req.getParameter("valor");
			if (tipo.endsWith("CPFCNPJ")){
				boolean  existe = false;
				existe = Cliente.ExisteCPFCNPJ(toCheckValue);
				
				// CPFCNPJ ja existe?
				if (existe) {
					// sim, entao diz q n pode usar
					output.append(Sistema.GeradorPaginas.MessageError(String.format("O CPFCNPJ \"%s\" ja esta sendo usado.", toCheckValue)));

				} else {
					output.append(Sistema.GeradorPaginas.MessageSuccess(String.format("O CPFCNPJ \"%s\" pode ser usado!", toCheckValue)));

				}
				
			}
		}
		System.out.println("outpute " + output);
        res.getWriter().print(output);

    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

    private Cliente PreencherDados(HttpServletRequest req) {
        Cliente cliente = new Cliente();


        return cliente;
    }
}
